<template>
  <div class="home-container">
    <div class="home-container-title">
      <h1>Welcome Range Builder</h1>
      <el-input
        v-model="searchWords"
        placeholder="请输入IP或者项目名称，支持模糊搜索"
        clearable
        class="input-with-select"
      >
        <el-button slot="append" icon="el-icon-search" @click="searchProjects" />
      </el-input>
    </div>
    <el-divider />
    <!-- 项目卡片展示 -->
    <el-row :gutter="20">
      <!-- 固定添加卡片 -->
      <el-col :span="6" class="card-col">
        <el-card class="add-card" @click.native="handleAddClick">
          <div class="add-icon">+</div>
          <div class="add-text">添加项目</div>
        </el-card>
      </el-col>

      <!-- 项目信息卡片 -->
      <el-col
        v-for="project in projectList"
        :key="project.projectId"
        :span="6"
        class="card-col"
      >
        <el-card class="project-card">
          <el-descriptions :title="project.projectName" :column="1" size="mini" border>
            <template slot="extra">
              <el-button type="text" size="small" @click="lookProjectDetail(project.projectRootDir)">查看详情
              </el-button>
            </template>
            <el-descriptions-item>
              <template slot="label">
                <i class="el-icon-house" />
                环境IP
              </template>
              {{ project.sshHost }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                <i class="el-icon-folder" />
                编译根目录
              </template>
              {{ project.projectRootDir }}
            </el-descriptions-item>
          </el-descriptions>
        </el-card>
      </el-col>
    </el-row>

    <!-- 导入弹窗 -->
    <el-dialog
      title="导入项目"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="30%"
    >
      <div style="position: relative">
        <el-button
          ref="downloadBtnRef"
          size="small"
          type="text"
          style="float: right;"
          @click="downloadTemplate"
        >
          下载模板
        </el-button>
        <RbUpload
          :file-list="fileList"
          accept=".xlsx, .xls"
          button-txt="点击选择项目-环境-组件信息模板"
          tip="只能上传 xlsx/xls 格式文件，且不超过5MB"
          @on-success="handleSuccess"
          @on-process="handleProcess"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import RbUpload from '@/layout/components/RbUpload.vue'
import { getProjectList, importProjectInfo } from '@/api/project'

export default {
  name: 'Home',
  components: { RbUpload },
  data() {
    return {
      searchWords: '',
      dialogVisible: false,
      // 文件列表
      fileList: [],
      loading: {},
      projectList: []
    }
  },
  async mounted() {
    await this.searchProjects()
  },
  methods: {
    async searchProjects() {
      const res = await getProjectList({ keywords: this.searchWords })
      if (res.data) {
        this.projectList = res.data
      }
    },
    handleAddClick() {
      this.dialogVisible = true
    },

    handleSuccess(buildFile) {
      this.fileList = []
      importProjectInfo({ fileId: buildFile.id }).then(res => {
        this.$store.dispatch('project/changeDynamicMenus')
        this.loading.close()
        location.reload()
      }).catch(err => {
        console.error(err)
        this.$message.error('导入模板失败')
        this.loading.close()
      })
    },

    handleProcess() {
      this.loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
    },

    lookProjectDetail(projectRootDir) {
      this.$router.push({ name: projectRootDir })
    },

    downloadTemplate() {
      this.$download('项目-环境-组件信息模板.xlsx')
    }
  }
}
</script>

<style lang="scss" scoped>
.home {
  &-container {
    margin: 30px;
  }

  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>

<style scoped>

.card-col {
  margin-bottom: 20px;
}

.add-card {
  cursor: pointer;
  text-align: center;
  height: 160px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.add-icon {
  font-size: 60px;
  color: #909399;
}

.add-text {
  color: #606266;
  margin-top: 10px;
}

.project-card {
  height: 160px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.project-card .title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 10px;
}

.project-card .content {
  color: #666;
  margin-bottom: 15px;
}

.project-card .time {
  color: #999;
  font-size: 12px;
}

.upload-demo {
  text-align: center;
}
.home-container-title {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
}
.home-container-title h1{
  margin-right: auto;
  flex-shrink: 0;
}
.home-container-title .input-with-select{
  width: 40%;
}
</style>
